package com.shuguang.medicalcare;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ListSelect extends AppCompatActivity  {
    public TextView phoneNumber;
    public TextView room;
    public Button changeBt;
    public Button roomBtn;
    public TextView setBtn;
    public LinearLayout listBox;
    public Context tempContext;
    public TextView listItem;
    public JSONArray listArr=new JSONArray();  //列表数据

    public SimpleDateFormat data;


    public String taskName;
    public String taskId="";
    public JSONArray taskArr;

    public List<Map<String,String>> tempList;
    public ScrollViewUpRefreshLayout myScroll;

    public Boolean ableClick = true;

    public String apkPath="";

//    public SharedPreferences sp;
//    public SharedPreferences.Editor ed;


    public TextView rsk1;
    public AlertDialog tempRskD;

    public ProgressDialog p;
    public String nodata;


    private mHandler mhandler=new mHandler(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_list_select);



//        try {
//            this.test();
//        } catch (PackageManager.NameNotFoundException e) {
//            e.printStackTrace();
//        }

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        tempContext = this;
//        sp=getSharedPreferences("list",MODE_PRIVATE);
//        ed=sp.edit();


        p=new ProgressDialog(this);

        this.init();



    }

    public void test() throws PackageManager.NameNotFoundException {
        PackageManager pm =getApplicationContext().getPackageManager();
        PackageInfo pi = pm.getPackageInfo(getApplicationContext().getPackageName(), 0);

        String versionName = pi.versionName;
        int versioncode = pi.versionCode;


        (new Thread(){
            @Override
            public void run() {

                //实例化  里面可能会出现异常，需要try  catch
                OkHttpClient client = new OkHttpClient();

                String path= MedicalCareApi.newPath+"callcenter/checkVersion";


//            http://49.234.94.134:8081/ccapp/callcenter/checkVersion?type=6&now_version=1.0.0

//                        Request request = new Request.Builder().url(path).build();
                Request.Builder reqBuild = new Request.Builder();
                HttpUrl.Builder urlBuilder =HttpUrl.parse(path)
                        .newBuilder();
                urlBuilder.addQueryParameter("type", "6");
                urlBuilder.addQueryParameter("now_version",String.valueOf(versionName)  );

                reqBuild.url(urlBuilder.build());
                Request request = reqBuild.build();



                try {

                    //Response是okhttp实例化，并且调用Request。执行后返回的结果
                    Response response = client.newCall(request).execute();
                    String s = response.body().string();//获取到的json数据
                    Log.i("path::", path);

                    JSONObject jsonObject =  new JSONObject(s);//转成json
                    int code = jsonObject.getInt("code");

//                    Log.i("jsonObject", String.valueOf(jsonObject.getInt("apk")) );
                    Log.i("版本号", s  );
                    if(code == 0){
//                                    无需更新




                    }

                    if(code == 1){

//                                    需要更新


                        Message msg = new Message();
                        msg.what=3;
                        mhandler.sendMessage(msg);

                        apkPath=jsonObject.getString("apk");

                        Log.i("apkName", apkPath);

                    }

                } catch (Exception e) {

                    e.printStackTrace();
                }




            }
        }).start();







    }

    @Override
    protected void onRestart() {
//        未被销毁的activity 重新 回到 栈顶
        super.onRestart();
        Log.i("info::", "onRestart: 我又回来了！+ " +taskId);

        try {
            SharedPreferences sp =getSharedPreferences("userinfo",MODE_PRIVATE);
            String str= sp.getString("list","");
            Log.i("错误33","错误"+ str);
//
            

            if(str.equals("")){
                getlistByTask(taskId);
            }else {
                listArr = new JSONArray(str);
                render();
            }



        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    //    Handle 静态内部类 防止内存泄漏
     private static class mHandler extends Handler {
        WeakReference<ListSelect> weakReference;
        public mHandler(ListSelect mainActivity){
            weakReference = new WeakReference<ListSelect>(mainActivity);
        }
        @Override
        public void handleMessage(Message msg){
            ListSelect mainActivity = weakReference.get();
            if(mainActivity != null){
    //            mainActivity.text_one.setText(String.valueOf(msg.arg1));
//                Log.i("收到信息了222222222222222222222222222222222222222222222222222222222");
                try {
                    if(msg.what == 1){
                        mainActivity.render();
                    }

                    if(msg.what == 2){

                       mainActivity.getlistByTask(mainActivity.taskId);
                    }

                    if(msg.what == 3){

                        mainActivity.showUpdateDialog();
                    }

                    if(msg.what == 99){
                        mainActivity.toastNodata();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    private void toastNodata() {
        p.hide();
        Toast.makeText(ListSelect.this, nodata, Toast.LENGTH_SHORT).show();
    }

    @SuppressLint("WrongConstant")
    public void render() throws JSONException {




        Log.i("数据render",listArr.toString());
                      listBox.removeAllViews();
                        for(int i =0;i<listArr.length();i++){
//                            Log.i("id", listArr.getJSONObject(i).get("text").toString());

                            String tempText= listArr.getJSONObject(i).get("name").toString();
                             Boolean stateV= listArr.getJSONObject(i).getBoolean("finishState");

                            View view = View.inflate(tempContext,R.layout.list_item,null);
                            TextView textV=(TextView) view.findViewById(R.id.itemTitle) ;
                            TextView state=(TextView) view.findViewById(R.id.stateBool) ;

                            textV.setText(tempText);
                            if(stateV){
                                state.setText("已选");
                              state.setBackgroundColor(Color.parseColor("#FF3C82"));
                            }else{
                                state.setText("未选");
                                state.setBackgroundColor(Color.parseColor("#AAAAAA"));
                            }


                            final  int finalI=i;
//                            列表  点击 监听
                            view.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
//                                    Toast.makeText(ListSelect.this, "点击了"+tempText, Toast.LENGTH_SHORT).show();

                                    try {



                                        String id=String.valueOf(listArr.getJSONObject(finalI).get("id"));
                                        String caller=String.valueOf(listArr.getJSONObject(finalI).get("caller"));
                                        String mobile=String.valueOf(listArr.getJSONObject(finalI).get("mobile"));
                                        Boolean stateV = listArr.getJSONObject(finalI).getBoolean("finishState");
                                        Log.i("itemId::", id);

                                        Log.i("item 状态::", stateV.toString());
//                                        Toast.makeText(getApplicationContext(),"Id:"+id,Toast.LENGTH_SHORT).show();
                                        if(stateV){
                                            Toast.makeText(getApplicationContext(),"该项已完成操作;请选择其他项！",Toast.LENGTH_SHORT).show();
                                        }else {
                                            Intent it= new Intent(getApplicationContext(), Detail.class);
                                            it.putExtra("id",id);
                                            it.putExtra("caller",caller);
                                            it.putExtra("mobile",mobile);
                                            startActivity(it);


                                        }







                                    } catch (Exception e) {
                                        ableClick=true;
                                        e.printStackTrace();
                                    }



                                }
                            });

                            listBox.addView(view);





                        }

                        p.dismiss();

//        Toast.makeText(ListSelect.this, "长度"+ String.valueOf(listBox.getChildCount()) , Toast.LENGTH_SHORT).show();
//                测试
//                   for(Map s : tempList){
//                            String tempText= s.get("text").toString();
//
//                            View view = View.inflate(tempContext,R.layout.list_item,null);
//                            TextView textV=(TextView) view.findViewById(R.id.itemTitle) ;
//                            textV.setText(tempText);
//
////                            final  int finalI=i;
//
//                            view.setOnClickListener(new View.OnClickListener() {
//                                @Override
//                                public void onClick(View v) {
//                                    Toast.makeText(ListSelect.this, "点击了"+tempText, Toast.LENGTH_SHORT).show();
//                                }
//                            });
//
//                            listBox.addView(view);
//                   }
//        测试








//                  myScroll.setVisibility( 0);
    }


    public void showUpdateDialog(){
        AlertDialog.Builder customizeDialog =
                new AlertDialog.Builder(ListSelect.this);
        View dialogView = LayoutInflater.from(ListSelect.this)
                .inflate(R.layout.updata,null);

        customizeDialog.setView(dialogView);
        customizeDialog.setCancelable(false);

        AlertDialog alertDialog =  customizeDialog.show();

        Button downBtn = (Button) dialogView.findViewById(R.id.download);

//        PackageManager pm =getApplicationContext().getPackageManager();
//        PackageInfo pi = null;
//        try {
//            pi = pm.getPackageInfo(getApplicationContext().getPackageName(), 0);
//            String versionName = pi.versionName;
//            int versioncode = pi.versionCode;
//
//            downBtn.setText(versionName);
//
//
////            Toast.makeText(MainActivity.this, "我又回来了？", Toast.LENGTH_SHORT).show();
//        } catch (PackageManager.NameNotFoundException e) {
//            e.printStackTrace();
//        }



        downBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Log.i("url+++", apkPath);
//                downBtn.setText(apkPath);
//                Toast.makeText(ListSelect.this, apkPath, Toast.LENGTH_SHORT).show();
//                apkPath









                new DownloadUtils(ListSelect.this,apkPath,"medicalCare.apk");

            }
        });

    }

    public static void renderGress(){}


    public void done(){
        room.setText(taskName);

    }
    @SuppressLint("WrongConstant")
    public void init(){
        SharedPreferences sp = getSharedPreferences("userinfo",MODE_PRIVATE);


        phoneNumber=(TextView) findViewById(R.id.phoneNumber);
        room =(TextView) findViewById(R.id.room);
        room.setText(sp.getString("task_name",""));

        changeBt=(Button) findViewById(R.id.changePhoneBtn);
        setBtn = (TextView)findViewById(R.id.setBtn);

        phoneNumber.setText(sp.getString("myPhone",""));


        roomBtn=(Button) findViewById(R.id.roomBtn);
        listBox=(LinearLayout) findViewById(R.id.list);
        myScroll=(ScrollViewUpRefreshLayout) findViewById(R.id.scrollRefresh);
        changeBt=(Button) findViewById(R.id.changePhoneBtn);
        roomBtn=(Button)findViewById(R.id.roomBtn);


        myScroll.setOnRefreshListener(new Runnable() {
            @Override
            public void run() {

                  String jsonStr=sp.getString("list","");

                try {
                    if(isComplete(new JSONArray(jsonStr))){
                        // 满足 每项的 finishState true --> 更新 sp userinfo-list 为 空串
                        listBox.removeAllViews();
                        SharedPreferences.Editor ed=sp.edit();
                        ed.putString("list","");
                        ed.commit();
                        getlistByTask(taskId);
                        myScroll.finishRefreshing();

                    }else {
  //                      不满足 完成释放刷新动作
                        myScroll.finishRefreshing();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        });


        changeBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showChangeNumDialog();
            }
        });

        roomBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    showChangeTaskDialog();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        setBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it= new Intent(getApplicationContext(), PersonSet.class);
                startActivity(it);
            }
        });




        String groupId=sp.getString("groupId","");

        (new Thread(){
            @Override
            public void run() {
//                http://wxwork.gzsg.cn/wxwork/callcenter/workbenchInviteTask?hosId=1&userId=LiHao&managerId=16296974261668



//                get 请求
                //实例化  里面可能会出现异常，需要try  catch
                OkHttpClient client = new OkHttpClient();
                //建立一个Request  通过Builder方法.Url里面填的就是json网址.build创建
//
//                String path="http://wxwork.gzsg.cn/wxwork/callcenter/workbenchInviteTask";
                String path=MedicalCareApi.newPath+ "callcenter/workbenchInviteTask";
                SharedPreferences sp= getSharedPreferences("userinfo",MODE_PRIVATE);


//                        Request request = new Request.Builder().url(path).build();
                Request.Builder reqBuild = new Request.Builder();
                HttpUrl.Builder urlBuilder =HttpUrl.parse(path)
                        .newBuilder();
                urlBuilder.addQueryParameter("hosId", "1");
                urlBuilder.addQueryParameter("userId", sp.getString("userId",""));
                urlBuilder.addQueryParameter("managerId", sp.getString("managerId",""));
                urlBuilder.addQueryParameter("callerId", sp.getString("callerId",""));
                reqBuild.url(urlBuilder.build());
                Request request = reqBuild.build();



                try {

                    //Response是okhttp实例化，并且调用Request。执行后返回的结果
                    Response response = client.newCall(request).execute();
                    String s = response.body().string();//获取到的json数据

                    Log.i("任务类型列表信息::", s);
                    JSONObject jsonObject =  new JSONObject(s);//转成json

                    Integer code = (Integer)jsonObject.getInt("code");

                    if(code == 0){

                       JSONArray arr =   jsonObject.getJSONArray("data");
                       taskArr =arr;
//                       测试
//                        ArrayList a=new ArrayList();
//                        JSONObject obj1= new JSONObject();
//                        obj1.put("task_id","1");
//                        obj1.put("task_name","测试1");
//                        JSONObject obj2= new JSONObject();
//                        obj2.put("task_id","2");
//                        obj2.put("task_name","测试2");
//                        a.add(obj1);
//                        a.add(obj2);
//
//                        taskArr =new JSONArray(a.toString());

//                        obj.put("finishState",false);








                       Log.i("string::", sp.getString("groupId",""));



//                        taskId =arr.getJSONObject(0).getString("task_id");



                       for(int i=0;i<arr.length();i++){
                          if( arr.getJSONObject(i).getString("group_id").equals(sp.getString("groupId",""))   ){
                              Log.i("ssss", arr.getJSONObject(i).getString("task_name"));
//                              room.setText(arr.getJSONObject(i).getString("task_name"));
                              Message msg = new Message();
//                              taskName=arr.getJSONObject(i).getString("task_name");
                              msg.what=2;
                              mhandler.sendMessage(msg);


                          }
                       }



                    }




                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }




            }


        }).start();






    }
    public Boolean isComplete(JSONArray arr){
        int len= arr.length();



        ArrayList temp=new ArrayList();
        JSONArray myArr = arr;
        for(int i=0;i<myArr.length();i++){
            try {
                if(myArr.getJSONObject(i).getBoolean("finishState")){
                    temp.add(new HashMap());
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        if(len == temp.size() ){
            return true;
        }else {
            return false;
        }



    }

    public void getlistByTask(String id) throws JSONException {



        SharedPreferences sp = getSharedPreferences("userinfo",MODE_PRIVATE);
        String jsonStr= sp.getString("list","");
        String callerId=sp.getString("callerId","");

        Log.i("锤子", "getlistByTask: "+jsonStr);




        if(!jsonStr.equals("") ){
            try{
                //  Toast.makeText(ListSelect.this, "点击了1", Toast.LENGTH_SHORT).show();

                listArr = new JSONArray(jsonStr);
                this.render();
            }catch (Exception e){
                e.printStackTrace();
            }

        }else if(id.equals("")){
            showChangeTaskDialog();
        }else  {

            p.setMessage("下组数据加载中...");
            p.setCancelable(false);
            p.show();

            new Thread(new Runnable() {
                @SuppressLint("ResourceType")
                @Override
                public void run() {
//                get 请求
                    //实例化  里面可能会出现异常，需要try  catch
                    OkHttpClient client = new OkHttpClient();
                    //建立一个Request  通过Builder方法.Url里面填的就是json网址.build创建
//                    http://wxwork.gzsg.cn/wxwork/callcenter/workbenchInviteTaskCustomerList?taskId=2&groupId=2&hosId=1&userId=LiHao&managerId=16296974261668

//                    String path="http://wxwork.gzsg.cn/wxwork/callcenter/workbenchInviteTaskCustomerList";
                    String path= MedicalCareApi.newPath+"callcenter/workbenchInviteTaskCustomerList";

                    SharedPreferences sp= getSharedPreferences("userinfo",MODE_PRIVATE);

//http://wxwork.gzsg.cn/wxwork/callcenter/workbenchInviteTaskCustomerList?taskId=2&groupId=2&hosId=1&userId=LiHao&managerId=16296974261668

                    Request.Builder reqBuild = new Request.Builder();
                    HttpUrl.Builder urlBuilder =HttpUrl.parse(path)
                            .newBuilder();
                    urlBuilder.addQueryParameter("hosId", "1");
                    urlBuilder.addQueryParameter("taskId" ,taskId);
//                    urlBuilder.addQueryParameter("callerId" ,callerId);

                    urlBuilder.addQueryParameter("groupId",sp.getString("groupId",""));
                    urlBuilder.addQueryParameter("userId", sp.getString("userId",""));
                    urlBuilder.addQueryParameter("managerId", sp.getString("managerId",""));
                    urlBuilder.addQueryParameter("callerId", sp.getString("callerId",""));

                    Log.i("参数", "hosId:1"+       "taskId:" + taskId +"groupId" +sp.getString("groupId","") + "userId:"+ sp.getString("userId","")+"managerId:"+sp.getString("managerId","")+"callerId:"+sp.getString("callerId",""));




                    reqBuild.url(urlBuilder.build());
                    Request request = reqBuild.build();


//                Request request = new Request.Builder().url(path).build();
                    try {
                        //Response是okhttp实例化，并且调用Request。执行后返回的结果
                        Response response = client.newCall(request).execute();
                        String s = response.body().string();//获取到的json数据

                        JSONObject jsonObject =  new JSONObject(s);//转成json



                        Integer code = (Integer)jsonObject.getInt("code");
                        Log.i("code::::", code.toString());
                        if(code == 0){
                            listArr = jsonObject.getJSONArray("data");//获取到名字为results的数据

//                            ArrayList a=new ArrayList();
//                            for(int k=0 ;k<10;k++){
//                                a.add(listArr.get(k));
//
//                            }
//
//                            listArr = new JSONArray(a.toString());



                            for(int i =0;i<listArr.length();i++){
                                JSONObject obj= listArr.getJSONObject(i);
                                obj.put("finishState",false);
//                                if(i<27){
//                                    obj.put("finishState",true);
//                                }else{
//                                    obj.put("finishState",false);
//                                }



                            }

                            Log.i("列表信息::", listArr.toString());

                            String str =  listArr.toString();
//                           ed.putString("list","123");

                            SharedPreferences list = getSharedPreferences("userinfo",MODE_PRIVATE);
                            SharedPreferences.Editor ed=list.edit();
                            ed.putString("list",str);
                            ed.commit();

//                           JSONArray arr = new JSONArray(str);
//
//                           Log.i("数据获取2", "list"+list.getString("list",""));
//
                            Message msg = new Message();
                            msg.what=1;
                            mhandler.sendMessage(msg);
                        }else{


                            Message msg = new Message();
                            nodata=(String) jsonObject.getString("msg");
                            msg.what=99;
                            mhandler.sendMessage(msg);
                        }




                    } catch (IOException | JSONException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }








    }














    public void showChangeNumDialog(){
        /* @setView 装入自定义View ==> R.layout.dialog_customize
         * 由于dialog_customize.xml只放置了一个EditView，因此和图8一样
         * dialog_customize.xml可自定义更复杂的View
         */
        AlertDialog.Builder customizeDialog =
                new AlertDialog.Builder(ListSelect.this);
        final View dialogViews = LayoutInflater.from(ListSelect.this)
                .inflate(R.layout.change_phone_dialog,null);
//        customizeDialog.setTitle("我是一个自定义Dialog");
//        customizeDialog.setCustomTitle()
        customizeDialog.setView(dialogViews);
        customizeDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 获取EditView中的输入内容
                         EditText edit_text = dialogViews.findViewById(R.id.editPhone);
                    if(edit_text.getText().toString().length()<11 ){
                        Toast.makeText(ListSelect.this,"手机号码错误", Toast.LENGTH_SHORT).show();
                    }else{
                        SharedPreferences sp = getSharedPreferences("userinfo",MODE_PRIVATE);
                        SharedPreferences.Editor ed= sp.edit();
                        ed.putString("myPhone",edit_text.getText().toString());
                        ed.commit();

                        phoneNumber.setText(edit_text.getText().toString());

                    }


                        


                    }
                });
        customizeDialog.show();

    }

//     任务列表
    public void showChangeTaskDialog() throws JSONException {
        /* @setView 装入自定义View ==> R.layout.dialog_customize
         * 由于dialog_customize.xml只放置了一个EditView，因此和图8一样
         * dialog_customize.xml可自定义更复杂的View
         */
        AlertDialog.Builder customizeDialog =
                new AlertDialog.Builder(ListSelect.this);
        final View dialogView = LayoutInflater.from(ListSelect.this)
                .inflate(R.layout.change_task_dialog,null);

        customizeDialog.setView(dialogView);
//        customizeDialog.setCancelable(false);

        tempRskD= customizeDialog.show();

        LinearLayout box = (LinearLayout) dialogView.findViewById(R.id.rg);
//


        for (int i=0;i<taskArr.length();i++){
            View view = View.inflate(this,R.layout.dialog_item,null);
             TextView tv = (TextView) view.findViewById(R.id.rask);

             tv.setText(taskArr.getJSONObject(i).getString("task_name"));



            final  int finalI=i;

            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(tempRskD != null){
//                        if(isComplete(listArr)) {
//
//                        }
                        try {


                           if( taskId.equals(taskArr.getJSONObject(finalI).getString("task_id"))   ){
                               Log.i("where", "1");
                               taskId=  taskArr.getJSONObject(finalI).getString("task_id");
                               room.setText(taskArr.getJSONObject(finalI).getString("task_name"));

                               SharedPreferences sp =getSharedPreferences("userinfo",MODE_PRIVATE);
                               SharedPreferences.Editor ed= sp.edit();

                               ed.putString("taskId",taskArr.getJSONObject(finalI).getString("task_id"));
                               ed.putString("task_name",taskArr.getJSONObject(finalI).getString("task_name"));
                               
                               ed.commit();

                               getlistByTask(taskId);
                           }else {
                               Log.i("where", "2");
//                               Toast.makeText(ListSelect.this, "当前还有其他任务!", Toast.LENGTH_SHORT).show();
                               taskId=  taskArr.getJSONObject(finalI).getString("task_id");
                               room.setText(taskArr.getJSONObject(finalI).getString("task_name"));

                               SharedPreferences sp =getSharedPreferences("userinfo",MODE_PRIVATE);
                               SharedPreferences.Editor ed= sp.edit();
                               ed.putString("taskId",taskArr.getJSONObject(finalI).getString("task_id"));
                               ed.putString("task_name",taskArr.getJSONObject(finalI).getString("task_name"));
                               ed.putString("list","");
                               ed.commit();


                               getlistByTask(taskId);







                           }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        tempRskD.dismiss();
                   }

                }
            });

             box.addView(view);



        }

    }

//    更新ui 需在主线程 完成
}